import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Patch, Post, Query } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { CreateDto, ListPageDto, UpdateDto } from './dto/wheel-prize.dto'
import { WheelPrizeService } from './wheel-prize.service'

@ApiTags('后台/二期/奖品管理')
@Controller('admin/wheel_prize')
export class WheelPrizeController {
  constructor(private readonly service: WheelPrizeService) {}

  /**
   * 奖品列表
   */
  @Get()
  page(@Query() query: ListPageDto) {
    const { pageNo, pageSize, ...rest } = query
    return this.service.findPage({ pageNo, pageSize, where: { ...rest, isDeleted: false } })
  }

  /**
   * 创建奖品
   */
  @Post()
  create(@Body() data: CreateDto) {
    return this.service.save(data)
  }

  /**
   * 修改奖品
   */
  @Patch(':wheelPrizeId')
  async update(@Param('wheelPrizeId') id: number, @Body() data: UpdateDto) {
    const findData = await this.service.findOne({ where: { wheelPrizeId: id, isDeleted: false } })
    if (!findData) {
      throw new HttpException('数据不存在', HttpStatus.NOT_FOUND)
    }
    return this.service.update(id, data)
  }

  /**
   * 删除奖品
   */
  @Delete(':wheelPrizeId')
  async delete(@Param('wheelPrizeId') id: number) {
    const findData = await this.service.findOne({ where: { wheelPrizeId: id, isDeleted: false } })
    if (!findData) {
      throw new HttpException('数据不存在', HttpStatus.NOT_FOUND)
    }
    return this.service.update(id, { isDeleted: true })
  }
}
